package cn.yunhe.controller;

import cn.yunhe.entity.Car;
import cn.yunhe.entity.Driver;
import cn.yunhe.service.ICarService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/after/AddCarServlet")
public class AddCarServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String cardId = request.getParameter("cardId");
        int driverId = Integer.parseInt(request.getParameter("driverId"));
        String cname = request.getParameter("cname");
        Double cmileage = Double.parseDouble(request.getParameter("cmileage"));
        int cvolume = Integer.parseInt(request.getParameter("cvolume"));
        String ddate = request.getParameter("ddate");
        Driver driver = new Driver(driverId);

        //添加輸入內容到 Car 車輛表中
        Car car = new Car(cardId,cname,cvolume,cmileage,driver,ddate);
        ICarService carService = new ICarService();
        if (carService.addCar(car)){
            response.sendRedirect("GetAllCarServlet");
        }else{
            response.sendRedirect("/after/feedbackpro-add.jsp");
        }
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }
}
